home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 22 / PC Actual CD 22.iso / progs / Netobj / netobj / t2.z / nav_canvas.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-04-29  |  5.0 KB  |  174 lines

  1. import java.awt.Canvas;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.Dimension;
  5. import java.awt.Event;
  6. import java.awt.FontMetrics;
  7. import java.awt.Graphics;
  8. import java.awt.Image;
  9. import java.awt.image.ImageObserver;
  10.  
  11. public class nav_canvas extends Canvas {
  12.    tree_entry[][] tree_grid;
  13.    int max_level;
  14.    int my_max_width;
  15.    int x_factor;
  16.    int y_factor;
  17.    Sitemapper parent_applet;
  18.    String current_selection;
  19.    boolean repeat_current;
  20.    private Image offScreenImage;
  21.    private Graphics offScreenGraphics;
  22.    private Dimension offScreenSize;
  23.  
  24.    nav_canvas(tree_entry[][] tree_grid, int max_level, int my_max_width, Sitemapper parent_applet) {
  25.       this.tree_grid = tree_grid;
  26.       this.max_level = max_level;
  27.       this.my_max_width = my_max_width;
  28.       this.parent_applet = parent_applet;
  29.    }
  30.  
  31.    tree_entry find_location(int x, int y) {
  32.       int grid_x = 0;
  33.       int grid_y = 0;
  34.       Dimension dim = ((Component)this).size();
  35.       this.x_factor = dim.width / (this.my_max_width + 1);
  36.       this.y_factor = dim.height / (this.max_level + 1);
  37.  
  38.       for(int i = 0; i < this.my_max_width + 2; ++i) {
  39.          if (x < i * this.x_factor) {
  40.             grid_x = i - 1;
  41.             break;
  42.          }
  43.       }
  44.  
  45.       for(int i = 0; i < this.max_level + 2; ++i) {
  46.          if (y < i * this.y_factor) {
  47.             grid_y = i - 1;
  48.             break;
  49.          }
  50.       }
  51.  
  52.       if (this.tree_grid[grid_x][grid_y] != null && this.tree_grid[grid_x][grid_y].publish) {
  53.          String temp_selection = this.tree_grid[grid_x][grid_y].get_url();
  54.          if (temp_selection.equals(this.current_selection)) {
  55.             this.repeat_current = true;
  56.          } else {
  57.             this.repeat_current = false;
  58.          }
  59.  
  60.          this.current_selection = temp_selection;
  61.          return this.tree_grid[grid_x][grid_y];
  62.       } else {
  63.          this.current_selection = null;
  64.          return null;
  65.       }
  66.    }
  67.  
  68.    public void paint(Graphics g) {
  69.       Dimension dim = ((Component)this).size();
  70.       g.setColor(Color.white);
  71.       g.fillRect(0, 0, dim.width, dim.height);
  72.       g.setColor(Color.black);
  73.       this.x_factor = dim.width / (this.my_max_width + 1);
  74.       this.y_factor = dim.height / (this.max_level + 1);
  75.  
  76.       for(int i = 0; i < this.my_max_width + 1; ++i) {
  77.          for(int j = 0; j < this.max_level + 1; ++j) {
  78.             if (this.tree_grid[i][j] != null && this.tree_grid[i][j].in_use) {
  79.                boolean publish = this.tree_grid[i][j].publish;
  80.                this.draw_node(g, i, j, dim, publish);
  81.                this.draw_branches(this.tree_grid[i][j], dim, g);
  82.             }
  83.          }
  84.       }
  85.  
  86.    }
  87.  
  88.    public boolean mouseUp(Event evt, int x, int y) {
  89.       this.parent_applet.show_url(this.current_selection);
  90.       return false;
  91.    }
  92.  
  93.    void draw_branches(tree_entry entry, Dimension dim, Graphics g) {
  94.       if (entry.children != null) {
  95.          tree_entry first = (tree_entry)entry.children.firstElement();
  96.          if (!first.in_use) {
  97.             return;
  98.          }
  99.  
  100.          tree_entry last = (tree_entry)entry.children.lastElement();
  101.          g.drawLine(first.grid_x * this.x_factor + this.x_factor / 4, first.grid_y * this.y_factor - this.y_factor / 2 + 5, last.grid_x * this.x_factor + this.x_factor / 4, last.grid_y * this.y_factor - this.y_factor / 2 + 5);
  102.          g.drawLine(entry.grid_x * this.x_factor + this.x_factor / 4, entry.grid_y * this.y_factor + this.y_factor / 2, entry.grid_x * this.x_factor + this.x_factor / 4, entry.grid_y * this.y_factor + this.y_factor / 2 + 5);
  103.  
  104.          for(int i = 0; i < entry.children.size(); ++i) {
  105.             tree_entry child = (tree_entry)entry.children.elementAt(i);
  106.             g.drawLine(child.grid_x * this.x_factor + this.x_factor / 4, child.grid_y * this.y_factor - this.y_factor / 2 + 5, child.grid_x * this.x_factor + this.x_factor / 4, child.grid_y * this.y_factor);
  107.          }
  108.       }
  109.  
  110.    }
  111.  
  112.    public final synchronized void update(Graphics theG) {
  113.       Dimension d = ((Component)this).size();
  114.       if (this.offScreenImage == null || d.width != this.offScreenSize.width || d.height != this.offScreenSize.height) {
  115.          this.offScreenImage = ((Component)this).createImage(d.width, d.height);
  116.          this.offScreenSize = d;
  117.          this.offScreenGraphics = this.offScreenImage.getGraphics();
  118.          this.offScreenGraphics.setFont(((Component)this).getFont());
  119.       }
  120.  
  121.       this.offScreenGraphics.fillRect(0, 0, d.width, d.height);
  122.       this.paint(this.offScreenGraphics);
  123.       theG.drawImage(this.offScreenImage, 0, 0, (ImageObserver)null);
  124.    }
  125.  
  126.    void draw_node(Graphics g, int i, int j, Dimension dim, boolean publish) {
  127.       if (publish) {
  128.          g.setColor(Color.yellow);
  129.       } else {
  130.          g.setColor(Color.lightGray);
  131.       }
  132.  
  133.       g.fillRect(i * this.x_factor, j * this.y_factor, this.x_factor / 2, this.y_factor / 2);
  134.       g.setColor(Color.black);
  135.       g.drawRect(i * this.x_factor, j * this.y_factor, this.x_factor / 2, this.y_factor / 2);
  136.    }
  137.  
  138.    public boolean mouseMove(Event e, int x, int y) {
  139.       tree_entry entry = this.find_location(x, y);
  140.       Graphics g = ((Component)this).getGraphics();
  141.       if (entry != null && entry.publish) {
  142.          if (!this.repeat_current) {
  143.             this.update(g);
  144.             g.setColor(Color.red);
  145.             g.fillRect(entry.grid_x * this.x_factor - 1, entry.grid_y * this.y_factor - 1, this.x_factor / 2 + 2, this.y_factor / 2 + 2);
  146.             g.setColor(Color.black);
  147.             FontMetrics fm = g.getFontMetrics(g.getFont());
  148.             int str_width = fm.stringWidth(entry.get_name());
  149.             Dimension d = ((Component)this).size();
  150.             int horiz_offset = 0;
  151.             int vert_offset = 0;
  152.             if (str_width + entry.grid_x * this.x_factor + this.x_factor / 2 + 2 > d.width) {
  153.                horiz_offset = str_width + entry.grid_x * this.x_factor + this.x_factor / 2 + 2 - d.width;
  154.             }
  155.  
  156.             if (entry.grid_y * this.y_factor - fm.getHeight() - 2 < 0) {
  157.                vert_offset = 0 - (entry.grid_y * this.y_factor - fm.getHeight() - 2);
  158.             }
  159.  
  160.             g.setColor(Color.white);
  161.             g.fillRect(entry.grid_x * this.x_factor + this.x_factor / 2 + 2 - horiz_offset, entry.grid_y * this.y_factor - fm.getHeight() - 2 + vert_offset, str_width, fm.getHeight());
  162.             g.setColor(Color.black);
  163.             g.drawString(entry.get_name(), entry.grid_x * this.x_factor + this.x_factor / 2 + 2 - horiz_offset, entry.grid_y * this.y_factor - 2 + vert_offset);
  164.             return true;
  165.          } else {
  166.             return false;
  167.          }
  168.       } else {
  169.          this.update(g);
  170.          return false;
  171.       }
  172.    }
  173. }
  174.